#include "iConfig.hpp"

#include <glf/core/semaphore.h>

#if GLF_ENABLE_DEBUGGER
#	include <glf/debugger/profiler.h>
#endif
 
#if __APPLE__
	#include "synchronization_posix_.hpp"
#else
	#include GLFi_HEADER_API(synchronization, GLF_API_THREAD)
#endif


namespace glf {

Semaphore::Semaphore(unsigned int initialValue, unsigned int maxValue) :
	m(GLF_NEW_IMPL(initialValue, maxValue))
{
	GLF_CHECK_IMPL_SIZE();
}

Semaphore::~Semaphore() {
	GLF_DELETE_IMPL();
}

int Semaphore::Wait() {
#if GLF_ENABLE_DEBUGGER
	debugger::SynchronizationScopeEvent dbgScopeEvent(debugger::sSemaphoreEventType, "[glf] Semaphore::Wait", (int)this);
#endif

	return m.Wait();
}

int Semaphore::TryWait() {
#if GLF_ENABLE_DEBUGGER
	int ret = m.TryWait();
	if(ret >= 0) {
		debugger::SynchronizationScopeEvent dbgScopeEvent(debugger::sSemaphoreEventType, "[glf] Semaphore::Wait", (int)this);
	}
	return ret;
#else
	return m.TryWait();
#endif
}

int Semaphore::Post() {
#if GLF_ENABLE_DEBUGGER
	debugger::SynchronizationScopeEvent dbgScopeEvent(debugger::sSemaphoreEventType, "[glf] Semaphore::Post", (int)this);
#endif

	return m.Post();
}

} //namespace glf
